# -*- coding: utf-8 -*-
import subprocess

from flask import json
import redis
from scrapy_redis.spiders import RedisSpider
from scrapy import Request, FormRequest
from spider.ops import Ops
from facility.reg_fn import get_number

from facility.reg_fn import get_number


class TaiyangSpider(RedisSpider):
    name = 'taiyang'
    redis_key = 'taiyang:start_urls'
    v8host = 'http://localhost:5000/'

    con = redis.Redis(host='localhost', port=6379, db=0)
    con.lpush(redis_key, v8host)

    def parse(self, response):
        for i in range(14):
            yield FormRequest(url='http://www.xjtyjr.com/lend/default.aspx',
                              formdata={
                                  "__VIEWSTATE": "/wEPDwUJNDc2NzE1OTQzD2QWAgIBD2QWAgIBDw8WAh4LUmVjb3JkY291bnQCI2RkZKHHwVJyw0s3gPZ1vV1LZxk4NsRF",
                                  "__VIEWSTATEGENERATOR": "1E288709",
                                  "__EVENTTARGET": "AspNetPager1",
                                  "__EVENTARGUMENT": "{}".format(i + 1),
                                  },
                              dont_filter=True, callback=self.parse_item)

    def parse_item(self, response):
        q = Ops(response)

        for i in range(q.get_num('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li')):
            a = i + 1
            link = q.get_attr('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li[{}]/a'.format(a),
                              'href').split("/")[-1]
            yield Request(url='http://www.xjtyjr.com/{}'.format(link),
                          dont_filter=True,
                          cookies={
                              'CNZZDATAWAP': "349430668-1532069245-http%3A%2F%2Fm2.people.cn",
                              '__jsluid': '9a7639815c78d8e0e4267fde32d06e25',
                              'cna': '8LvAE6TA71YCAdpYf/eetH8k',
                              '验证码接受': '5876',
                              'Users': 'Id=7656&UserName=18582851425&service=&type=&currentrole=%e4%b8%aa%e4%ba%ba&usertime=2018/8/13 14:57:58&isdelegate=2&flag=Duser'
                          },
                          headers={
                              "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8",
                              "Accept-Encoding": "gzip, deflate",
                              "Accept-Language": "zh-CN,zh;q=0.9",
                              "Cache-Control": "max-age=0",
                              "Connection": "keep-alive",
                              # "Cookie":'验证码接受=5876; Users=Id=7656&UserName=18582851425&service=&type=&currentrole=%e4%b8%aa%e4%ba%ba&usertime=2018/8/13 14:57:58&isdelegate=2&flag=Duser',
                              "Host": "www.xjtyjr.com",
                              'Upgrade-Insecure-Requests': "1",
                              "User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36"
                          },
                          callback=self.parse_detail)

        # for i in range(q.get_num('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li')):
        #     a = i+1
        #     name = q.get_str('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li[{}]/a/div[1]/span'.format(a))
        #     rate = q.get_str('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li[{}]/a/div[2]/span[1]'.format(a))
        #     cycle = q.get_str('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li[{}]/a/div[3]/span'.format(a))
        #     unit = q.get_str('//*[@id="frmSearch"]/div[5]/div/div/div[2]/div/div[1]/div[7]/ul[1]/li[{}]/a/div[3]/span/small'.format(a))
        #     data = name+' '+'金额'+' '+rate+' '+cycle+unit
        #     subprocess.call("echo  {} >>太阳信诺.txt".format(data), shell=True)

    def parse_detail(self, response):

        q = Ops(response)

        name = q.get_str('//*[@id="plan-basic-panel"]/div/div[1]/h3')
        amount = q.get_str('//*[@id="plan-basic-panel"]/div/div[1]/ul/li[1]/span[2]/em')
        rate = q.get_str('//*[@id="expected-rate"]/em')
        start = [i for i in get_number(name) if len(str(i)) > 6][-1]
        start = start[0:4] + "-" + start[4:6] + "-" + start[6:8]
        cycle = q.get_str('//*[@id="plan-basic-panel"]/div/div[1]/ul/li[4]/span[2]/em')
        way = q.get_str('//*[@id="plan-basic-panel"]/div/div[1]/ul/li[4]/span[4]')

        data = str(name) + " " + str(amount) + " " + str(rate) + " " + str(start) + " " + str(cycle) + " " + str(way)
        subprocess.call("echo  '{}' >> 泰阳.txt".format(data), shell=True)
